package CeShi;
import java.io.*;
import java.util.*;
/**
 * 统计指定目录下，所有文件中单词出现的次数，并按出现次数降序，数次相同，按照单词首字母升序
 */
public class Test03 {
    static HashMap<String,Integer> map;
    public static void main(String[] args) {
        File file=new File("E:\\测试3");
        StringBuffer str=new StringBuffer();
        File file1=new File("E:\\测试3\\Test.txt");
        if(!file.isDirectory()){
            System.out.println("文件传入有误！");
            return;
        }
        //读取文件内容
        str=duQu(file);
        //拆分统计每个单词出现的次数
        tongJi(str);
        //先排序再将文件中出现单词的次数写入另一个文本文件中
        cunFang(file1);

    }

    private static void cunFang(File file) {
        List<Map.Entry<String,Integer>> list=new ArrayList<Map.Entry<String, Integer>>(map.entrySet());
        Collections.sort(list, new Comparator<Map.Entry<String, Integer>>() {
            @Override
            public int compare(Map.Entry<String, Integer> o1, Map.Entry<String, Integer> o2) {
                if(o2.getValue().equals(o1.getValue())){
                    char a=o1.getKey().charAt(0);
                    char b=o2.getKey().charAt(0);
                    return a-b;
                }
                return o2.getValue().compareTo(o1.getValue());
            }
        });
        BufferedWriter bw=null;
        try {
            bw= new BufferedWriter(new FileWriter(file));
            for (Map.Entry<String,Integer> mapping:list) {
                bw.write(mapping.getKey()+"-->"+mapping.getValue()+"次");
                bw.newLine();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }finally{
            if (null!=bw){
                try {
                    bw.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
    private static void tongJi(StringBuffer str) {
        String[] strs=str.toString().split(" ");
        map=new HashMap<String,Integer>();
        for(String key:strs){
            if(!map.containsKey(key)){
                map.put(key, 1);
            }else{
                map.put(key,map.get(key)+1);
            }
        }
    }
    private static StringBuffer duQu(File file) {
        StringBuffer str=new StringBuffer();
        File[] files=file.listFiles();
        BufferedReader br=null;
        for (File fi:files) {
            try {
                br=new BufferedReader(new FileReader(fi));
                while(true){
                   String line= br.readLine();
                   if (line==null){
                       break;
                   }
                   str.append(line);
                }
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }finally {
                if (null!=br){
                    try {
                        br.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }
        }
        return str;
    }
}

